/**
*  
* @Function 数据质量报告服务
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.service.impl    
* @FileName DataQualityServiceImpl.java    
* @Date 2018年7月11日上午10:22:36
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/
package com.py.credit.service.impl;

import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.py.credit.dao.DataQualityDao;
import com.py.credit.dao.FunctionReportDao;
import com.py.credit.entity.DataQualityCollectionEntity;
import com.py.credit.entity.DataQualityDetailEntity;
import com.py.credit.entity.DataQualityEntity;
import com.py.credit.service.DataQualityService;
import com.py.credit.service.FunctionReportService;
import com.py.credit.utils.FileUtil;
import com.py.credit.utils.HtmlToPdfUtils;
import com.py.credit.utils.SQLBuilder;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.common.Page;

import freemarker.cache.StringTemplateLoader;

/**
 * 
 * @Function 数据质量报告服务
 * @ClassName DataQualityServiceImpl
 * @Date 2018年7月11日 上午10:22:36
 * @Author Min DongXv
 * 
 */
@Service
public class FunctionReportServiceImpl implements FunctionReportService {

	// 工商信息表
	private static final String REGISTER_TABLE_NAME = "1000000010014_LFsxNK85Kf";
	// 自然人行政处罚
	private static final String ZRR_PUNISH = "1000010010004_KFPULZpF7D";
	// 法人行政处罚
	private static final String FR_PUNISH = "1000000010002_XSni4zSzP0";
	// 自然人行政许可
	private static final String ZRR_PERMIT = "1000010010003_vVwjDLoDWy";
	// 法人行政许可
	private static final String FR_PERMIT = "1000000010001_3ioj4GeZQU";

	private static final long DAY_MILL_SECONDS = 24 * 60 * 60 * 1000L;

	@Autowired
	private DataQualityDao dataQualityDao;
	@Autowired
	private FunctionReportDao functionReportDao;

	/**
	 * 
	 * @Function 分页查询数据质量报告
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName dataQualityListByConditionForPage
	 * @param columns
	 * @param additionalColumns
	 * @param page
	 * @param condition
	 * @return
	 * @Date 2018年7月11日 下午2:31:39
	 * @Author Min DongXv
	 *
	 */
	@Override
	public List<Map<String, Object>> functionReportListByConditionForPage(List<String> columns, String[] additionalColumns,
			Page page, String condition) {
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		return functionReportDao.functionReportListByConditionForPage(columns, additionalColumns, page, conditions);
	}


	/**
	 *
	 * @Function 下载数据质量报告PDF
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName pdfDownload
	 * @param fileName
	 * @param dateRange
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	@Override
	public void pdfDownload(String id, HttpServletResponse response) {
		String path = functionReportDao.getFunctionReportPath(id);
		FileUtil.downloadFile(path, response);
	}

	/**
	 * 
	 * @Function 生成数据质量报告
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName dataQualityReport
	 * @param dateRange
	 * @param response
	 * @param request
	 * @throws Exception 
	 * @Date 2018年7月16日 下午5:11:29
	 * @Author Min DongXv
	 *
	 */
	@Override
	@Transactional
	public String functionReportReport(String pdfJsPath, String absolutepath,String googlePath, String startTime,String endTime,String type,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		String fileName = createFileName(startTime);
		// 数据质量报告前台地址
		StringBuffer url = request.getRequestURL();
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length())
				.append(request.getServletContext().getContextPath()).append("/").toString();
		// 本地测试使用
		// String tempContextUrl = "http://localhost:3000" +
		// request.getServletContext().getContextPath() + "/";
		String pageUrl = tempContextUrl + "functionReport/dataQualityPdfReport.do?startTime="+startTime+"&endTime=" + endTime;
		// 数据质量报告文件地址
		String pdfPath = absolutepath + "/functionReport/" + startTime;
		
		boolean googleConvent = HtmlToPdfUtils.googleConvent(pdfJsPath,
				pageUrl, pdfPath, fileName + ".pdf", null, true,
				googlePath);
		if(googleConvent) {
			functionReportDao.addFunctionReport("",startTime,fileName+".pdf",pdfPath,type);
			return (pdfJsPath+"+"+pageUrl+"+"+pdfPath+"+"+fileName);
		}else {
			throw new Exception(pdfJsPath+"+"+pageUrl+"+"+pdfPath+"+"+fileName);
		}
	}

	/**
	 *
	 * @Function 生成文件名
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName createFileName
	 * @param dateRange
	 *            期数
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	private String createFileName(String dateRange) {
		long head = System.currentTimeMillis() % (DAY_MILL_SECONDS);
		long body = Thread.currentThread().getId();
		int foot = ThreadLocalRandom.current().nextInt(9999);
		return "公共信用信息平台运行情况报告DQ-" + dateRange +"-" + foot;
	}

	/**
	 *
	 * @Function 上传转换PDF
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName createDataQualityReport
	 * @param
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	// private RestServiceResult<Object> upLoadPdf(HttpServletRequest request,
	// String fileName, String pageUrl,
	// String pdfPath) {
	// RestServiceResult<Object> result = new RestServiceResult<Object>();
	// String basePath = request.getContextPath();
	// String path = request.getScheme() + "://" + request.getServerName() + ":"
	// +
	// request.getServerPort() + basePath;
	// String commond = "google-chrome --headless --disable-gpu --print-to-pdf="
	// +
	// pdfPath + fileName + ".pdf " + path
	// + pageUrl;
	// try {
	// Runtime.getRuntime().exec(commond);
	// return result;
	// } catch (Exception e) {
	// ExceptionLogger.error("DataQualityServiceImpl#upLoadPdf",
	// "上传转换PDF异常！ERROR："
	// + e.getMessage(), e);
	// result.setCode(RestServiceResult.CODE_UNKNOW);
	// result.setMsg("上传转换PDF异常！ERROR：" + e.getMessage());
	// return result;
	// }
	// }

	/**
	 *
	 * @Function 生成数据质量报告
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName createDataQualityReport
	 * @param dateRange
	 *            期数
	 * @throws ParseException 
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	private Map<String, Object> createDataQualityReport(String startT, String endT) throws ParseException {
		// 返回数据Map
		Map<String, Object> data = new HashMap<String, Object>(16);
		// 问题列表集合
		List<String> problemList = new ArrayList<String>(2);
		// 获取时间数据
		Map<String, String> dates = dateDel(startT,endT);
		String startTime = dates.get("startTime");
		String endTime = dates.get("endTime");
		// 获取总体信息
		Map<String, Object> total = dataQualityDao.getTotalData(startTime, endTime,null);
		int tcount = Integer.parseInt(String.valueOf(total.get("tcount")));
		int scount = Integer.parseInt(String.valueOf(total.get("scount")));
		int fcount = Integer.parseInt(String.valueOf(total.get("fcount")));
		int wcount = Integer.parseInt(String.valueOf(total.get("wcount")));
		int rcount = Integer.parseInt(String.valueOf(total.get("rcount")));
		String srate = "100%";
		String frate = "100%";
		String wrate = "100%";
		String rrate = "100%";
		DecimalFormat df = new DecimalFormat("0.00");
		if (tcount != 0) {
			srate = endWithZero(df.format((double) scount * 100 / tcount)) + "%";
			frate = endWithZero(df.format((double) fcount * 100 / tcount)) + "%";
			wrate = endWithZero(df.format((double) wcount * 100 / tcount)) + "%";
			double rate = (double) rcount * 100 / tcount;
			rrate = endWithZero(df.format(rate)) + "%";
			// 重复率>5%写入问题列表
			if (rate > 5) {
				String p = "重复数据过多,重复率：" + rrate;
				problemList.add(p);
			}
		}
		// 计算填充比率数据
		total.put("srate", srate);
		total.put("frate", frate);
		total.put("wrate", wrate);
		total.put("rrate", rrate);
		data.put("total", total);
		// 同比和环比数据
		String yearIncrease = "100%";
		String monthIncrease = "100%";
		if (tcount != 0) {
			// 获取去年同月的数据
			int y = dataQualityDao.getTotalCount(dates.get("yStartTime"), dates.get("yEndTime"),null);
			// 获取上月的数据
			int m = dataQualityDao.getTotalCount(dates.get("mStartTime"), dates.get("mEndTime"),null);
			if (y > 0) {
				yearIncrease = endWithZero(df.format((double) tcount * 100 / y)) + "%";
			}
			if (m > 0) {
				monthIncrease = endWithZero(df.format((double) tcount * 100 / m)) + "%";
			}
		}
		data.put("yearIncrease", yearIncrease);
		data.put("monthIncrease", monthIncrease);
		// 资源表top5
		List<Map<String, Object>> targetSumTop = dataQualityDao.getTargetSumTop(startTime, endTime);
		data.put("targetSumTop", targetSumTop);
			// 部门采集top5
			List<Map<String, Object>> deptSumTop = dataQualityDao.getQuyuSumTop(startTime, endTime);
			data.put("deptSumTop", deptSumTop);

			// 数据归集质量分析
			List<DataQualityCollectionEntity> dataCollectionList = dataQualityDao.getQuyuDataCollectionList(startTime,
					endTime,null);
			data.put("dataCollectionList", dataCollectionList);

			// 获取部门入库率数据
			List<Map<String, Object>> deptRateTop = dataQualityDao.deptQuRateList(startTime, endTime, "D");
			List<Map<String, Object>> deptRateLast = dataQualityDao.deptQuRateList(startTime, endTime, "A");
			data.put("deptRateTop", deptRateTop);
			data.put("deptRateLast", deptRateLast);

			// 获取基本信息的成功入库数
 			int registerTotal = dataQualityDao.getJbSCount(startTime, endTime);
			// 获取基本信息的列表数据
			List<DataQualityCollectionEntity> registerList = dataQualityDao.getDataJbCollectionList(startTime, endTime);

			data.put("registerTotal", registerTotal);
			data.put("registerList", registerList);

			
			// 获取双公示数据
			List<String> resourceIds = new ArrayList<String>(4);
			resourceIds.add(ZRR_PERMIT);
			resourceIds.add(FR_PERMIT);
			resourceIds.add(ZRR_PUNISH);
			resourceIds.add(FR_PUNISH);

			// 获取双公示总体数据
			Map<String, Object> administrationTotal = dataQualityDao.getTotalData(startTime, endTime, resourceIds);
			
			List<DataQualityCollectionEntity> administrationListFrxk = dataQualityDao.getQuyuDataCollectionList(startTime, endTime, 210000);
			List<DataQualityCollectionEntity> administrationListFrcf = dataQualityDao.getQuyuDataCollectionList(startTime, endTime, 210001);
			List<DataQualityCollectionEntity> administrationListZrrxk = dataQualityDao.getQuyuDataCollectionList(startTime, endTime, 210002);
			List<DataQualityCollectionEntity> administrationListZrrcf = dataQualityDao.getQuyuDataCollectionList(startTime, endTime, 210003);
			
			int permitNum = 0;
			int punishNum = 0;
			int tNum = 0;
			List<Map<String, Object>> scatterList = new ArrayList<Map<String, Object>>();
			
			List<Map<String, Object>> pDataFrxk = dataQualityDao.getTrateCount(startTime, endTime, "1000000010001_3ioj4GeZQU","210000");
			List<Map<String, Object>> pDataFrcf = dataQualityDao.getTrateCount(startTime, endTime, "1000000010002_XSni4zSzP0","210001");
			List<Map<String, Object>> pDataZrrxk = dataQualityDao.getTrateCount(startTime, endTime, "1000010010003_vVwjDLoDWy","210002");
			List<Map<String, Object>> pDataZrrcf = dataQualityDao.getTrateCount(startTime, endTime, "1000010010004_KFPULZpF7D","210003");
			
			Map<String, Integer> pDataMapFrxk = new HashMap<String, Integer>();
			if (!CollectionUtils.isEmpty(pDataFrxk)) {
				for (Map<String, Object> pm : pDataFrxk) {
					if (!CollectionUtils.isEmpty(pm) && null != pm.get("num")) {
						int n = Integer.parseInt(String.valueOf(pm.get("num")));
						pDataMapFrxk.put(String.valueOf(pm.get("name")) , n);
						tNum = tNum + n;
					}
				}
			}
			
			Map<String, Integer> pDataMapFrcf = new HashMap<String, Integer>();
			if (!CollectionUtils.isEmpty(pDataFrcf)) {
				for (Map<String, Object> pm : pDataFrcf) {
					if (!CollectionUtils.isEmpty(pm) && null != pm.get("num")) {
						int n = Integer.parseInt(String.valueOf(pm.get("num")));
						pDataMapFrcf.put(String.valueOf(pm.get("name")) , n);
						tNum = tNum + n;
					}
				}
			}
			
			Map<String, Integer> pDataMapZrrxk = new HashMap<String, Integer>();
			if (!CollectionUtils.isEmpty(pDataZrrxk)) {
				for (Map<String, Object> pm : pDataZrrxk) {
					if (!CollectionUtils.isEmpty(pm) && null != pm.get("num")) {
						int n = Integer.parseInt(String.valueOf(pm.get("num")));
						pDataMapZrrxk.put(String.valueOf(pm.get("name")) , n);
						tNum = tNum + n;
					}
				}
			}
			
			Map<String, Integer> pDataMapZrrcf = new HashMap<String, Integer>();
			if (!CollectionUtils.isEmpty(pDataZrrcf)) {
				for (Map<String, Object> pm : pDataZrrcf) {
					if (!CollectionUtils.isEmpty(pm) && null != pm.get("num")) {
						int n = Integer.parseInt(String.valueOf(pm.get("num")));
						pDataMapZrrcf.put(String.valueOf(pm.get("name")) , n);
						tNum = tNum + n;
					}
				}
			}
			
			
			if (!CollectionUtils.isEmpty(administrationListFrxk)) {
				for(DataQualityCollectionEntity e : administrationListFrxk){
					int snum = e.getScount();
					permitNum = permitNum + e.getTcount();
					// 默认及时率的100%
					String trate = "0%";
					Integer tnum = 0;
					
					if (snum != 0) {
						String tip = e.getDeptName();
						if (pDataMapFrxk.containsKey(tip)) {
							tnum = pDataMapFrxk.get(tip);
							double rate = (double) tnum / e.getScount();
							// 及时率不大于100%
							if (rate > 1) {
								rate = 1L;
							}
							trate = endWithZero(df.format(rate * 100)) + "%";
						} else {
							trate = "0%";
						}
					}
					e.setTrate(trate);
					// 填充散点图信息
					Map<String, Object> m = new HashMap<String, Object>(3);
					m.put("deptName", trate);
					m.put("tcount", e.getTcount());
					m.put("scount", tnum);
					scatterList.add(m);
				}
			}
			
			if (!CollectionUtils.isEmpty(administrationListFrcf)) {
				for(DataQualityCollectionEntity e : administrationListFrcf){
					punishNum = punishNum + e.getTcount();
					int snum = e.getScount();
					// 默认及时率的100%
					String trate = "0%";
					Integer tnum = 0;
					
					if (snum != 0) {
						String tip = e.getDeptName();
						if (pDataMapFrcf.containsKey(tip)) {
							tnum = pDataMapFrcf.get(tip);
							double rate = (double) tnum / e.getScount();
							// 及时率不大于100%
							if (rate > 1) {
								rate = 1L;
							}
							trate = endWithZero(df.format(rate * 100)) + "%";
						} else {
							trate = "05";
						}
					}
					e.setTrate(trate);
					// 填充散点图信息
					Map<String, Object> m = new HashMap<String, Object>(3);
					m.put("deptName", trate);
					m.put("tcount", e.getTcount());
					m.put("scount", tnum);
					scatterList.add(m);
				}
			}
			
			if (!CollectionUtils.isEmpty(administrationListZrrxk)) {
				
				for(DataQualityCollectionEntity e : administrationListZrrxk){
					permitNum = permitNum + e.getTcount();
					int snum = e.getScount();
					// 默认及时率的100%
					String trate = "0%";
					Integer tnum = 0;
					
					if (snum != 0) {
						String tip = e.getDeptName();
						if (pDataMapZrrxk.containsKey(tip)) {
							tnum = pDataMapZrrxk.get(tip);
							double rate = (double) tnum / e.getScount();
							// 及时率不大于100%
							if (rate > 1) {
								rate = 1L;
							}
							trate = endWithZero(df.format(rate * 100)) + "%";
						} else {
							trate = "0%";
						}
					}
					e.setTrate(trate);
					// 填充散点图信息
					Map<String, Object> m = new HashMap<String, Object>(3);
					m.put("deptName", trate);
					m.put("tcount", e.getTcount());
					m.put("scount", tnum);
					scatterList.add(m);
				}
			}
			
			if (!CollectionUtils.isEmpty(administrationListZrrcf)) {
				
				for(DataQualityCollectionEntity e : administrationListZrrcf){
					punishNum = punishNum + e.getTcount();
					int snum = e.getScount();
					// 默认及时率的100%
					String trate = "0%";
					Integer tnum = 0;
					
					if (snum != 0) {
						String tip = e.getDeptName();
						if (pDataMapZrrcf.containsKey(tip)) {
							tnum = pDataMapZrrcf.get(tip);
							double rate = (double) tnum / e.getScount();
							// 及时率不大于100%
							if (rate > 1) {
								rate = 1L;
							}
							trate = endWithZero(df.format(rate * 100)) + "%";
						} else {
							trate = "0%";
						}
					}
					e.setTrate(trate);
					// 填充散点图信息
					Map<String, Object> m = new HashMap<String, Object>(3);
					m.put("deptName", trate);
					m.put("tcount", e.getTcount());
					m.put("scount", tnum);
					scatterList.add(m);
				}
			}
			
			
			
			// 行政许可数量
			data.put("permitNum", permitNum);
			// 行政处罚数量
			data.put("punishNum", punishNum);
			// 双公示总数
			int sgsTotal = permitNum + punishNum;
			String trate = "100%";
			if (sgsTotal != 0) {
				double tr = (double) tNum / sgsTotal;
				trate = endWithZero(df.format(tr * 100)) + "%";
				if (tr < 0.9) {
					problemList.add("双公示数据及时率过低,及时率:" + trate);
				}
			}
			administrationTotal.put("trate", trate);
			// 行政处罚总体数据
			data.put("administrationTotal", administrationTotal);
			// 行政处罚列表数据
			data.put("administrationListFrxk", administrationListFrxk);
			data.put("administrationListFrcf", administrationListFrcf);
			data.put("administrationListZrrxk", administrationListZrrxk);
			data.put("administrationListZrrcf", administrationListZrrcf);
			// 散点图
			data.put("scatterList", scatterList);
			if (CollectionUtils.isEmpty(problemList)) {
				problemList.add("无");
			}
			// 问题列表
			data.put("problemList", problemList);

		return data;
	}
	/**
	 * 
	 * @Function 创建数据质量报告HTML
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName createPdfHtml
	 * @param dateRange
	 * @return
	 * @throws ParseException 
	 * @Date 2018年7月31日 下午6:07:10
	 * @Author Min DongXv
	 *
	 */
	@Override
	public String createPdfHtml(String startTime,String endTime) throws ParseException {
		StringBuilder sb = new StringBuilder();
		Map<String, Object> m = createDataQualityReport(startTime,endTime);
		String content = JSONObject.toJSONString(m);
		if(!StringUtils.isEmpty(content)) {
			JSONObject jb = JSONObject.parseObject(content);
			JSONObject total = jb.getJSONObject("total");
			JSONArray deptSumTop = jb.getJSONArray("deptSumTop");
			JSONArray dataCollectionList = jb.getJSONArray("dataCollectionList");
			JSONArray targetSumTop = jb.getJSONArray("targetSumTop");
			// JSONArray scatterList = jb.getJSONArray("scatterList");
			// String punishNum = jb.getString("punishNum");
			// JSONArray deptRateTop = jb.getJSONArray("deptRateTop");
			JSONArray registerList = jb.getJSONArray("registerList");
			JSONObject administrationTotal = jb.getJSONObject("administrationTotal");
			JSONArray administrationList = jb.getJSONArray("administrationList");
			JSONArray problemList = jb.getJSONArray("problemList");
			// JSONArray deptRateLast = jb.getJSONArray("deptRateLast");
			sb.append("<!DOCTYPE html>");
			sb.append("<html>");
			sb.append("<head>");
			sb.append("<meta charset='utf-8'>");
			sb.append("<title>质量报告</title>");
			sb.append("<meta name='renderer' content='webkit'>");
			sb.append("<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>");
			sb.append(
					"<meta name='viewport' content='width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0'>");
			sb.append("<script src='/web_site/start/js/echarts.min.js'></script>");
			sb.append(
					"<style>*{margin:0;padding:0}.clearBoth{zoom:1}.clearBoth:after{display:block;clear:both;content:\"\";visibility:hidden;height:0}body{font-family:\"Helvetica Neue\",Helvetica,Tahoma,Arial,sans-serif;font-size:12px}button,div,i,input,li,ul{-webkit-box-sizing:border-box;box-sizing:border-box}li,ul{list-style:none}table{border-collapse:collapse;border-spacing:0}.lay-dataQualityReportpreview{padding-bottom:20px;width:1200px}.lay-dataQualityReportpreview .previewtitle{width:193px;height:58px;font-size:32px;font-weight:700;margin-left:422px;margin-top:47px}.lay-dataQualityReportpreview .previewsub{width:140px;height:17px;margin-left:452px;font-size:13px}.lay-dataQualityReportpreview .previewsubtitle{font-size:24px;font-weight:700;margin-left:80px;margin-top:50px}.lay-dataQualityReportpreview .previewdata{margin-left:80px;margin-top:0;font-size:15px}.lay-dataQualityReportpreview .previewdata p{font-size:15px;font-weight:400;margin-top:5px;margin-left:0}.lay-dataQualityReportpreview .previewdata i{font-size:14px;font-weight:400}.lay-dataQualityReportpreview .previewthtitle{font-size:24px;font-weight:700;margin-left:100px;margin-top:30px}.lay-dataQualityReportpreview .previewsubitem table{margin-left:100px;margin-top:20px;height:70px}.lay-dataQualityReportpreview .previewsubitem th{width:110px;height:35px;font-size:15px;font-weight:400;text-align:center}.lay-dataQualityReportpreview .previewsubitem td{width:110px;height:35px;font-size:15px;font-weight:400;text-align:center}.lay-dataQualityReportpreview .previewnumber{color:#1e9fff}.lay-dataQualityReportpreview .preview-item{font-size:15px;line-height:30px;margin-left:100px;padding-top:15px;padding-bottom:0}</style>");
			sb.append("</head>");
			sb.append("<body>");
			sb.append("<div class='lay-dataQualityReportpreview'>");
			sb.append("<div class='previewtop'>");
			sb.append("<div class='previewtitle' style='width:250px;'>数据质量报告");
			sb.append("</div>");
			sb.append("<div class='previewsub'>报告期数：" + startTime + "期");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("<div class='previewcontent'>");
			sb.append("<div class='partone' style='height:600px'>");
			sb.append("<div class='previewsubtitle'>一、数据质量总体情况");
			sb.append("</div>");
			sb.append("<div class='previewdata'>");
			sb.append("<p class='preview-item'>本期数据采集：");
			sb.append("<i style='color:red;font-weight:600;'>" + total.getString("tcount"));
			sb.append("</i>条；");
			sb.append("</p>");
			sb.append("<p class='preview-item'>数据采集同比增长：");
			sb.append("<i style='font-weight:600;'>").append(jb.getString("yearIncrease"))
					.append("</i>；&nbsp;&nbsp;&nbsp;&nbsp;环比增长");
			sb.append("<i style='font-weight:600;'>").append(jb.getString("monthIncrease")).append("</i>；");
			sb.append("</p>");
			sb.append("<p class='preview-item'>本期数据采集量最多的信息资源表TOP5：");
			if (null != targetSumTop && targetSumTop.size() > 0) {
				for (int i = 0; i < targetSumTop.size(); i++) {
					JSONObject j = targetSumTop.getJSONObject(i);
					sb.append("<span>" + j.getString("tableName") + "</span>；");
				}
			}
			sb.append("</p>");
			sb.append("<p class='preview-item'>本期数据采集量最多的部门TOP5：");
			if (null != deptSumTop && deptSumTop.size() > 0) {
				for (int i = 0; i < deptSumTop.size(); i++) {
					JSONObject j = deptSumTop.getJSONObject(i);
					sb.append("<span>" + j.getString("deptName") + "</span>；");
				}
			}
			sb.append("</p>");
			sb.append("</div>");
			sb.append("<div style='float:left;width:450px;height:350px;margin-left: 80px;'>");
			sb.append("<div id='previewchart1' style='float:left;width:450px;height:350px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("<div style='float:left;width:350px;height:350px;'>");
			sb.append(
					"<div id='previewchart2' style='position:absolute;left:550px;width:350px;height:350px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("<div class='parttwo'>");
			sb.append("<div class='previewquality'>");
			sb.append("<div class='previewsubtitle' style='margin-top:0px'>二、数据报送质量分析");
			sb.append("</div>");
			sb.append("<div class='previewsubitem' style=' overflow:hidden;'>");
			sb.append("<div class='previewthtitle'>2.1、数据归集质量分析");
			sb.append("</div>");
			sb.append("<div class='previewdata'>");
			sb.append("<p class='preview-item'>在此报告期间，约");
			sb.append("<i class='previewnumber'>").append(total.getString("deptNum")).append("</i>家信源单位提供了数据，提供记录总数：");
			sb.append("<i class='previewnumber'>").append(total.getString("tcount")).append("</i>条记录，符合归集质量要求");
			sb.append("<i class='previewnumber'>").append(total.getString("scount")).append("</i>条，归集质量比例：");
			sb.append("<i class='previewnumber'>").append(total.getString("srate")).append("</i>，").append("</p>");
			sb.append("<p class='preview-item'>归集异常记录数据：");
			sb.append("<i class='previewnumber'>").append(total.getString("fcount")).append("</i>，占归集总数比例：");
			sb.append("<i class='previewnumber'>").append(total.getString("frate")).append("</i>，重复记录数据：");
			sb.append("<i class='previewnumber'>").append(total.getString("rcount")).append("</i>，占归集总数比例：");
			sb.append("<i class='previewnumber'>").append(total.getString("rrate")).append("</i>，待关联记录数据：");
			sb.append("<i class='previewnumber'>").append(total.getString("wcount")).append("</i>，占归集总数比例：");
			sb.append("<i class='previewnumber'>").append(total.getString("wrate")).append("</i>");
			sb.append("</p>");
			sb.append("<p class='preview-item'>归集质量明细：");
			sb.append("</p>");
			sb.append("</div>");
			sb.append("<table class='dataTable' border='1'>");
			sb.append("<tr>");
			sb.append("<th>序号</th>");
			sb.append("<th>报送单位</th>");
			sb.append("<th>报送数据</th>");
			sb.append("<th>入库数</th>");
			sb.append("<th>异常数</th>");
			sb.append("<th>重复数</th>");
			sb.append("<th>待关联</th>");
			sb.append("<th>入库率</th>");
			sb.append("</tr>");
			if (null != dataCollectionList && dataCollectionList.size() > 0) {
				for (int i = 0; i < dataCollectionList.size(); i++) {
					JSONObject j = dataCollectionList.getJSONObject(i);
					sb.append("<tr>");
					sb.append("<td>" + (i + 1) + "</td>");
					sb.append("<td>" + j.getString("deptName") + "</td>");
					sb.append("<td>" + j.getString("tcount") + "</td>");
					sb.append("<td>" + j.getString("scount") + "</td>");
					sb.append("<td>" + j.getString("fcount") + "</td>");
					sb.append("<td>" + j.getString("rcount") + "</td>");
					sb.append("<td>" + j.getString("wcount") + "</td>");
					sb.append("<td>" + j.getString("srate") + "</td>");
					sb.append("</tr>");
				}
			}

			sb.append("</table>");
			sb.append("<div class='previewdata' style='margin-top:50px;'>本期数据采集")
					.append("<i class='previewnumber' style='color:red'>").append(total.getString("tcount"))
					.append("</i>条；&nbsp;&nbsp;&nbsp;&nbsp;其中，入库数：").append("<i class='previewnumber'>")
					.append(total.getString("scount")).append("</i>条；异常数：").append("<i class='previewnumber'>")
					.append(total.getString("fcount")).append("条</i>；重复数：").append("<i class='previewnumber'>")
					.append(total.getString("rcount")).append("条</i>；待关联数：").append("<i class='previewnumber'>")
					.append(total.getString("wcount")).append("条</i>；入库率：");
			sb.append("<iclass='previewnumber'>").append(total.getString("srate")).append("</i>；</div>");
			sb.append("<div style='margin-left:80px; overflow:auto;'>");
			sb.append("<div id='chart3' style='float:left;width:250px;height:250px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("<div id='chart3a' style='float:left;margin:65px 0 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart3b' style='float:left;margin:65px 0 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart3c' style='float:left;margin:65px 0 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart3d' style='float:left;margin:65px 0 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart3e' style='float:left;margin:65px 0 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append(
					"<p style='font-size:24px;margin-top:70px;margin-left:350px'>部门质量通报<span style='font-size:15px;'></span></p>");
			sb.append("<div style='margin-left:80px;overflow:auto;'>");
			sb.append("<div id='chart4' style='float:left;width:440px;height:300px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append(
					"<div id='chart4a' style='float:left;width:440px;height:300px;margin-top:30px;position:absolute;left:550px;'>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("<div class='previewsubitem'>");
			sb.append("<div class='previewthtitle'>2.2、主数据分析</div>");
			sb.append(
					"<div class='previewdata' style='margin-top:70px;'>在此报告期间，企业主数据采用各级工商部门的“工商注册信息”，产生有效企业主数据量为<span style='color:blue'>")
					.append(jb.getString("registerTotal")).append("</span>，分别产生如下企业主数据</div>");
			sb.append("<table class='dataTable' style='margin-left:100px;' border='1'>");
			sb.append("<tr>");
			sb.append("<th>序号</th>");
			sb.append("<th>报送单位</th>");
			sb.append("<th>报送数据</th>");
			sb.append("<th>入库数</th>");
			sb.append("<th>异常数</th>");
			sb.append("<th>重复数</th>");
			sb.append("<th>正确率</th>");
			sb.append("</tr>");
			if (null != registerList && registerList.size() > 0) {
				for (int i = 0; i < registerList.size(); i++) {
					JSONObject j = registerList.getJSONObject(i);
					sb.append("<tr>");
					sb.append("<td>" + (i + 1) + "</td>");
					sb.append("<td>" + j.getString("deptName") + "</td>");
					sb.append("<td>" + j.getString("tcount") + "</td>");
					sb.append("<td>" + j.getString("scount") + "</td>");
					sb.append("<td>" + j.getString("fcount") + "</td>");
					sb.append("<td>" + j.getString("rcount") + "</td>");
					sb.append("<td>" + j.getString("srate") + "</td>");
					sb.append("</tr>");
				}
			}
			sb.append("</table>");
			sb.append("</div>");
			sb.append("<div class='previewsubitem'>");
			sb.append("<div class='previewthtitle'>2.3、专题数据分析</div>");
			sb.append("<div class='previewdata' style='margin-top:10px;'>");
			sb.append("<p class='preview-item'>在此报告期间，企业主数据采用各级部门的“行政许可”、“行政处罚”数据，产生有效双公示专题数据量为"
					+ administrationTotal.getString("tcount") + "，数据及时率为" + administrationTotal.getString("trate")
					+ "，</p>");
			sb.append("<p class='preview-item'>分别产生如下企业主数据</p>");
			sb.append("</div>");
			sb.append("<table class='dataTable' style='width:1000px;margin-left:100px;' border='1'>");
			sb.append("<tr>");
			sb.append("<th>序号</th>");
			sb.append("<th>信息资源表</th>");
			sb.append("<th>报送单位</th>");
			sb.append("<th>报送数据</th>");
			sb.append("<th>入库数</th>");
			sb.append("<th>异常数</th>");
			sb.append("<th>重复数</th>");
			sb.append("<th>正确率</th>");
			sb.append("<th>数据及时率</th>");
			sb.append("</tr>");
			if (null != administrationList && administrationList.size() > 0) {
				for (int i = 0; i < administrationList.size(); i++) {
					JSONObject j = administrationList.getJSONObject(i);
					sb.append("<tr>");
					sb.append("<td>" + (i + 1) + "</td>");
					sb.append("<td>" + j.getString("tableName") + "</td>");
					sb.append("<td>" + j.getString("deptName") + "</td>");
					sb.append("<td>" + j.getString("tcount") + "</td>");
					sb.append("<td>" + j.getString("scount") + "</td>");
					sb.append("<td>" + j.getString("fcount") + "</td>");
					sb.append("<td>" + j.getString("rcount") + "</td>");
					sb.append("<td>" + j.getString("srate") + "</td>");
					sb.append("<td>" + j.getString("trate") + "</td>");
					sb.append("</tr>");
				}
			}
			sb.append("</table>");
			sb.append("<div style='margin-left:80px;overflow:auto;'>");
			sb.append("<div id='chart5' style='float:left;width:330px;height:250px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("<div id='chart5a' style='float:left;margin:65px 20px 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart5b' style='float:left;margin:65px 20px 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart5c' style='float:left;margin:65px 20px 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("<div id='chart5d' style='float:left;margin:65px 20px 0 0;width:170px;height:170px'>");
			sb.append("</div>");
			sb.append("</div>");

			sb.append("<div style='margin-left:80px;overflow:auto;'>");
			sb.append("<div id='chart6' style='float:left;width:400px;height:300px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("<div id='chart7' style='float:left;width:600px;height:300px;margin-top:30px;'>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("<div class='partthree'>");
			sb.append("<div class='previewsubitem'>");
			sb.append("<div class='previewsubtitle'>三、数据质量改进建议</div>");
			sb.append("<div class='previewthtitle'>3.1、数据归集存在的问题</div>");
			sb.append("<div class='previewdata' style='margin-top:30px;'>在此报告期间，数据归集的主要存在以下问题：</div>");
			sb.append("<div class='previewproblem'>");
			sb.append("<ul>");
			if (null != problemList && problemList.size() > 0) {
				for (int i = 0; i < problemList.size(); i++) {
					String j = problemList.getString(i);
					sb.append("<li class='preview-item'>" + j + "</li>");
				}
			} else {
				sb.append("<li class='preview-item'>无</li>");
			}
			sb.append("</ul>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</div>");
			sb.append("</body>");
			sb.append("<script>");
			sb.append("var callbackData = ").append(jb).append(";");
			sb.append(
					"var previewchart1_xAxis=[],previewchart1_data=[];callbackData.targetSumTop.map(function(t){previewchart1_xAxis.push(t.tableName),previewchart1_data.push(t.count)}),console.log(previewchart1_xAxis);var myChart=echarts.init(document.getElementById(\"previewchart1\")),options={title:{text:\"本期数据采集量最多的信息资源表TOP5\",x:\"center\"},textStyle:{fontSize:11},color:[\"#79aab0\",\"#79aab0\",\"#79aab0\",\"#79aab0\",\"#79aab0\"],xAxis:{data:previewchart1_xAxis,splitNumber:6,axisLabel:{textStyle:{fontSize:11},rotate:-45},axisTick:{show:!1}},yAxis:{name:\"数量/条\",splitNumber:1,axisLabel:{formatter:\"{value}\"},axisline:{show:!0,lineStyle:{color:\"#ddd\"}},splitLine:{show:!0}},series:[{type:\"bar\",data:previewchart1_data,barWidth:30,itemStyle:{normal:{label:{show:!0,position:\"top\",formatter:\"{c}\"}}}}]};myChart.setOption(options);var previewchart2_xAxis=[],previewchart2_data=[];callbackData.deptSumTop.map(function(t){previewchart2_xAxis.push(t.deptName),previewchart2_data.push(t.count)});myChart=echarts.init(document.getElementById(\"previewchart2\")),options={title:{text:\"本期数据采集量最多的部门TOP5\",x:\"center\"},textStyle:{fontSize:11},xAxis:{data:previewchart2_xAxis,axisLabel:{textStyle:{fontSize:11},rotate:-45}},yAxis:{name:\"数量/条\",splitNumber:1,axisLabel:{formatter:\"{value}\"},splitLine:{show:!0}},color:[\"#91ca81\",\"#91ca81\",\"#91ca81\",\"#91ca81\",\"#91ca81\"],series:[{type:\"bar\",data:previewchart2_data,barWidth:30,itemStyle:{normal:{label:{show:!0,position:\"top\",formatter:\"{c}\"}}}}]};myChart.setOption(options);var chart3_data=[{value:callbackData.total.tcount/1e3}];console.log(callbackData);options={title:{text:\"本期数据采集总量\",subtext:\"单位：千\\n\",x:\"center\",itemGap:50},tooltip:{formatter:\"{a} <br/>{b} : {c}\",fontSize:20},series:[{type:\"gauge\",startAngle:240,endAngle:-60,min:0,max:100,precision:0,splitNumber:5,itemStyle:{normal:{color:\"#aaa\"}},pointer:{length:\"80%\",width:5,color:\"#999\"},detail:{show:!0,backgroundColor:\"auto\",width:80,height:30,offsetCenter:[\"0%\",100],formatter:\"{value}\",textStyle:{color:\"#eee\",fontSize:20}},axisLine:{show:!0,lineStyle:{width:10}},splitLine:{show:!0,length:10,lineStyle:{color:\"#eee\",width:2,type:\"solid\"}},axisLabel:{show:!0,formatter:function(t){switch(t+\"\"){case\"0\":return\"0\";case\"20\":return\"0.02\";case\"40\":return\"0.04\";case\"60\":return\"0.06\";case\"80\":return\"0.08\";case\"100\":return\"0.10\";default:return\"\"}}},data:chart3_data}]};(myChart=echarts.init(document.getElementById(\"chart3\"))).setOption(options);var chart3a_value=[callbackData.total.scount/1e3];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart3a\")),options={series:[{name:\"入库数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,cursor:{fontSize:20},label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#1e9fff\",data:[{value:chart3a_value,name:chart3a_value+\"K\\n\\n入库数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#508DFF\"},{offset:1,color:\"#26C5FE\"}])}}}]}]};myChart.setOption(options);var chart3b_value=[callbackData.total.fcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart3b\")),options={series:[{name:\"异常数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#5fb878\",data:[{value:chart3b_value,name:chart3b_value+\"\\n\\n异常数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#63E587\"},{offset:1,color:\"#5FE2E4\"}])}}}]}]};myChart.setOption(options);var chart3c_value=[callbackData.total.rcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart3c\")),options={series:[{name:\"重复数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#AB82FF\",data:[{value:chart3c_value,name:chart3c_value+\"\\n\\n重复数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#ffbfff\"},{offset:1,color:\"#AB82FF\"}])}}}]}]};myChart.setOption(options);var chart3d_value=[callbackData.total.wcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart3d\")),options={series:[{name:\"待关联数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#91ca81\",data:[{value:chart3d_value,name:chart3d_value+\"\\n\\n待关联数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#508DFF\"},{offset:1,color:\"#26C5FE\"}])}}}]}]};myChart.setOption(options);var chart3e_value1=[callbackData.total.scount],chart3e_value2=[callbackData.total.tcount],chart3e_value=[];chart3e_value.push(Math.floor(chart3e_value1/chart3e_value2*100)),console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart3e\")),options={series:[{name:\"入库率\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\",formatter:\"{c}%\\n\\n入库率\",precision:2},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},data:[{value:chart3e_value,precision:2,itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#ffb5b5\"},{offset:1,color:\"#ff7575\"}])}}}]}]};myChart.setOption(options);var chart4_yAxis=[],chart4_data=[];callbackData.dataCollectionList.map(function(t){chart4_yAxis.push(t.deptName)}),callbackData.deptRateTop.map(function(t){chart4_data.push(100*t.srate)});myChart=echarts.init(document.getElementById(\"chart4\")),options={title:{text:\"部门最高TOP5\",x:\"center\"},xAxis:[{type:\"value\",position:\"top\",splitNumber:5,axisLabel:{formatter:\"{value}%\"},show:!1,inverse:{default:!0}}],yAxis:[{type:\"category\",position:\"right\",data:chart4_yAxis,max:4,axisLabel:{textStyle:{fontSize:9},rotate:-40},inverse:{default:!0},splitLine:{show:!0}}],color:[\"#6f8cde\",\"#6f8cde\",\"#6f8cde\",\"#6f8cde\",\"#6f8cde\"],series:[{name:\"部门最高TOP5\",type:\"bar\",barWidth:30,data:chart4_data,itemStyle:{normal:{label:{show:!0,position:\"left\",formatter:\"{c}%\"}}}}]};myChart.setOption(options);var chart4a_yAxis=[],chart4a_data=[];callbackData.deptRateLast.map(function(t){chart4a_yAxis.push(t.deptName),chart4a_data.push(t.srate*100)});myChart=echarts.init(document.getElementById(\"chart4a\")),options={title:{text:\"部门最低TOP5\",x:\"center\"},xAxis:[{type:\"value\",position:\"top\",axisLabel:{formatter:\"{value}%\"},splitNumber:5,min:0,max:100,show:!1}],yAxis:[{type:\"category\",data:chart4a_yAxis,axisLabel:{textStyle:{fontSize:9},rotate:-40},max:4,inverse:{default:!0},splitLine:{show:!0}}],series:[{name:\"部门最低TOP5\",type:\"bar\",barWidth:30,data:chart4a_data,itemStyle:{normal:{label:{show:!0,position:\"right\",formatter:\"{c}%\",precision:2}}}}]};myChart.setOption(options);var chart5_data=[{value:callbackData.administrationTotal.tcount/1e3}];console.log(callbackData);options={title:{subtext:\"单位：千\",x:\"center\"},tooltip:{formatter:\"{a} <br/>{b} : {c}\"},toolbox:{show:!0,feature:{mark:{show:!0}}},series:[{type:\"gauge\",startAngle:240,endAngle:-60,itemStyle:{normal:{color:\"#aaa\"}},min:0,max:100,precision:0,splitNumber:5,pointer:{length:\"80%\",width:5,color:\"#999\"},axisLine:{show:!0,lineStyle:{color:[[.2,\"#c23531\"],[.8,\"#63869e\"],[1,\"#91c7ae\"]],width:10}},splitLine:{show:!0,length:10,lineStyle:{color:\"#eee\",width:2,type:\"solid\"}},axisLabel:{show:!0,formatter:function(t){switch(t+\"\"){case\"0\":return\"0\";case\"20\":return\"0.02\";case\"40\":return\"0.04\";case\"60\":return\"0.06\";case\"80\":return\"0.08\";case\"100\":return\"0.10\";default:return\"\"}}},detail:{show:!0,backgroundColor:\"auto\",width:80,height:30,offsetCenter:[\"0%\",100],formatter:\"{value}\",textStyle:{color:\"#eee\",fontSize:20}},data:chart5_data}]};(myChart=echarts.init(document.getElementById(\"chart5\"))).setOption(options);var chart5a_value=[callbackData.administrationTotal.scount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart5a\")),options={series:[{name:\"入库数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#1e9fff\",data:[{value:chart5a_value,name:chart5a_value+\"\\n\\n入库数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#508DFF\"},{offset:1,color:\"#26C5FE\"}])}}}]}]};myChart.setOption(options);var chart5b_value=[callbackData.administrationTotal.fcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart5b\")),options={series:[{name:\"异常数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#5fb878\",data:[{value:chart5b_value,name:chart5b_value+\"\\n\\n异常数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#63E587\"},{offset:1,color:\"#5FE2E4\"}])}}}]}]};myChart.setOption(options);var chart5c_value=[callbackData.administrationTotal.rcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart5c\")),options={series:[{name:\"重复数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#AB82FF\",data:[{value:chart5c_value,name:chart5c_value+\"\\n\\n重复数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#ffbfff\"},{offset:1,color:\"#ab82ff\"}])}}}]}]};myChart.setOption(options);var chart5d_value=[callbackData.administrationTotal.wcount];console.log(callbackData);myChart=echarts.init(document.getElementById(\"chart5d\")),options={series:[{name:\"待关联数\",type:\"pie\",radius:[\"55%\",\"70%\"],avoidLabelOverlap:!1,label:{normal:{show:!0,position:\"center\"},emphasis:{show:!0,textStyle:{fontSize:\"15\",fontWeight:\"bold\"}}},labelLine:{normal:{show:!1}},color:\"#01aaed\",data:[{value:chart5b_value,name:chart5d_value+\"\\n\\n待关联数\",itemStyle:{normal:{color:new echarts.graphic.LinearGradient(1,0,0,0,[{offset:0,color:\"#508DFF\"},{offset:1,color:\"#26C5FE\"}])}}}]}]};myChart.setOption(options);var chart6_data=[{value:callbackData.permitNum,name:callbackData.permitNum+\"\\n行政许可\"},{value:callbackData.punishNum,name:callbackData.punishNum+\"\\n行政处罚\"}];console.log(callbackData);options={title:{text:\"本期行政许可与行政处罚数据占比\",x:\"center\"},tooltip:{trigger:\"item\",formatter:\"{b} : {c} ({d}%)\"},calculable:!0,series:[{type:\"pie\",radius:\"55%\",center:[\"50%\",\"60%\"],data:chart6_data,label:{normal:{position:\"inner\"}}}],color:[\"#d06a67\",\"#6f8cde\"]};(myChart=echarts.init(document.getElementById(\"chart6\"))).setOption(options);var chart7_xAxis=[],chart7_data=[],chart7_name=[];callbackData.scatterList.map(function(t){chart7_xAxis.push(t.tcount),chart7_data.push(t.scount),chart7_name.push(t.deptName)});myChart=echarts.init(document.getElementById(\"chart7\")),options={title:{text:\"本期双公示延时率\",x:\"center\"},tooltip:{trigger:\"axis\",showDelay:0,axisPointer:{show:!0,type:\"cross\",lineStyle:{type:\"dashed\",width:1}},formatter:function(t){return chart7_name[t[0].dataIndex]}},xAxis:{data:chart7_xAxis,name:\"总条数\",axisLabel:{textStyle:{fontSize:9}}},yAxis:{type:\"value\",name:\"按期上报正确条数\",scale:!0,splitNumber:5,axisLabel:{formatter:\"{value}\",textStyle:{fontSize:9},show:!0},min:0,splitLine:{show:!0}},color:\"#6f8cde\",series:[{type:\"scatter\",data:chart7_data,name:chart7_name}]};myChart.setOption(options);");
			sb.append("</script>");
			sb.append("</html>");
		}
		return sb.toString();
	}
	/**
	 *
	 * @Function 时间处理
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName dateDel
	 * @param dateRange
	 *            期数
	 * @param endT 
	 * @throws ParseException 
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	private Map<String, String> dateDel(String startT, String endT) throws ParseException {
		Map<String, String> dateMap = new HashMap<String, String>(6);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		SimpleDateFormat edf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		Calendar calendar = Calendar.getInstance();
		String dateRange = "";
		if(endT == null || "".equals(endT)) {
			dateRange = startT;
		}else {
			dateRange = new SimpleDateFormat("yyyyMM").format(df.parse(startT));;
		}
		if (StringUtils.isNotBlank(dateRange)) {
			String year = dateRange.substring(0, 4);
			String month = dateRange.substring(4, 6);
			calendar.set(Calendar.YEAR, Integer.parseInt(year));
			calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		}
		// 设置为当月第一天
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		Date startDate = calendar.getTime();
		dateMap.put("startTime", sdf.format(startDate));
		// 设置为当月最后一天
		if(endT == null || "".equals(endT)) {
			dateMap.put("endTime", edf.format(endT));
		}else {
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			Date endDate = calendar.getTime();
			dateMap.put("endTime", edf.format(endDate));
		}
		// 去年 开始时间
		calendar.setTime(startDate);
		calendar.add(Calendar.YEAR, -1);
		dateMap.put("yStartTime", sdf.format(calendar.getTime()));
		// 去年 结束时间
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		dateMap.put("yEndTime", edf.format(calendar.getTime()));
		// 上月 开始时间
		calendar.setTime(startDate);
		calendar.add(Calendar.MONTH, -1);
		dateMap.put("mStartTime", sdf.format(calendar.getTime()));
		// 上月 结束时间
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		dateMap.put("mEndTime", edf.format(calendar.getTime()));
		return dateMap;
	}

	/**
	 *
	 * @Function 生成pdf路径
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName buildDataQuality
	 * @param fileName
	 * @param dateRange
	 * @Date 2018年7月11日 下午5:25:09
	 * @Author Min DongXv
	 *
	 */
	private String buildDataQuality(String absolutepath, String fileName, String dateRange) {
		return absolutepath + "/dataQuality/" + dateRange + File.separator + fileName + ".pdf";
	}

	/**
	 *
	 * @Function 去除计算的0
	 * @FileName DataQualityServiceImpl.java
	 * @MethodName endWithZero
	 * @param rate
	 *            比率
	 * @Author Min DongXv
	 *
	 */
	private String endWithZero(String rate) {
		if (rate.contains(".")) {
			if (rate.endsWith("0")) {
				rate = rate.substring(0, rate.length() - 1);
			}
			if (rate.endsWith("0")) {
				rate = rate.substring(0, rate.length() - 2);
			}
		}
		return rate;
	}


	@Override
	public void dataQualityReport(String pdfzlbg, String startTime, String endTime,
			HttpServletResponse response, HttpServletRequest request, 
			String pdfJsPath, String googlePath,String absolutepath,String type) throws ParseException {
		DataQualityEntity entity = new DataQualityEntity();
		DataQualityDetailEntity dEntity = new DataQualityDetailEntity();
		// 创建数据质量报告
		Map<String, Object> m = createDataQualityReport(startTime, endTime);
		// 获取总体数据
		Map<String, Object> total = (Map<String, Object>) m.get("total");
		String content = JSONObject.toJSONString(m);
		String fileName = createFileName(startTime);
		// 数据质量报告前台地址
		// StringBuffer url = request.getRequestURL();
		// String tempContextUrl = url.delete(url.length() -
		// request.getRequestURI().length(), url.length())
		// .append(request.getServletContext().getContextPath()).append("/").toString();
		// 本地测试使用
		// String tempContextUrl = "http://localhost:3000" +
		// request.getServletContext().getContextPath() + "/";
		// String pageUrl = tempContextUrl +
		// "dataQuality/dataQualityPdfReport.do?dateRange=" + dateRange;
		// 数据质量报告文件地址
		// String pdfPath = absolutepath + "/dataQuality/" + dateRange +
		// File.separator;
		String pdfPath = absolutepath + "/function/" + startTime;
		String uuid = UuidUtil.getInstance().getUUid();
		// 上传转换文件
		boolean googleConvent = HtmlToPdfUtils.googleConvent(pdfJsPath,
				pdfzlbg+ "/id=" + uuid + "/startTime=" + startTime+ "/endTime=" + endTime + "/name=运行报告", pdfPath, fileName + ".pdf", null, true,
				googlePath);
		// boolean pdfResult = HtmlToPdfUtils.convent(wkpdfToolPath, pageUrl,
		// pdfPath, fileName, null);
				
		// 新增数据质量报告详情
		functionReportDao.addFunctionReportDetail(uuid,startTime+"~"+endTime,content);
		// 上传成功填入文件名称
		if (googleConvent) {
			functionReportDao.addFunctionReport(uuid,startTime+"~"+endTime,fileName+".pdf",pdfPath,type);
		}
		
	}


	@Override
	public DataQualityDetailEntity dataQualityDetail(String id) {
		
		return functionReportDao.getFunctionReportDetail(id);
	}
}
